package com.haidong.config;

import com.haidong.controller.Code;
import com.haidong.controller.Result;
import com.haidong.exception.BusinessException;
import com.haidong.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

// 用于表示当前类为REST风格的异常处理器通知,要被springMVCConfig扫描
@RestControllerAdvice
public class ProjectExceptionAdvice {
    // @ExceptionHandler用于处理当前处理类对应的异常类
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        // 记录日志,发送给运维
        // 发送邮件给开发,ex对象发送给开发人员
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    // 除了自定义异常,还保留Exception的异常处理,用于处理非预期异常
    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
//        System.out.println("捕捉到异常");
        return new Result(Code.SYSTEM_UNKNOW_ERR,null,"代码出现异常");
    }
}
